<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>生成器替换迭代器</title>
</head>
<body>
    <script>
        //第一个版本
    //   let Arr = ["cq","bn","pp"];
    //   function* createArrayIterator(arr)
    //   {
    //       for(let i = 0;i<arr.length;i++)
    //       {
    //         yield arr[i];
    //       }
    //   }
    //   let generator = createArrayIterator(Arr);
    //   console.log(generator.next().value);
    //   console.log(generator.next().value);
    //   console.log(generator.next().value);

    //生成器
      let Arr = ["cq","bn","pp"];
      function* createArrayIterator(arr)
      {
        yield*arr;
      }
      let generator = createArrayIterator(Arr);
      console.log(generator.next().value);
      console.log(generator.next().value);
      console.log(generator.next().value);
    
    //生成器对象，对标迭代器对象
    let obj = {
        name:"cq",
        friends:["cq","mwl","kobe","james"],
        [Symbol.iterator]:function*()
        {
           yield*this.friends;
        }
    }
   for(name of obj)
   {
    console.log(name);
   }

   
   //类里面添加生成器
   class Person{
     constructor(name)
     {
        this.name = name;
     }

     #friends = ["james","curry","kobe","tatumu"];

     *[Symbol.iterator]()
     {
        yield* this.#friends;
     }
   }
   const p1 = new Person("cq");
   for(friend of p1)
   {
    console.log(friend);
   }
      
    </script>
</body>
</html>